﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UserControlLib
{
    /// <summary>
    /// NumberScrollingControl.xaml 的交互逻辑
    /// </summary>
    public partial class NumberScrollingControl : UserControl, INotifyPropertyChanged
    {
       private TranslateTransform _translateTransform;
        public NumberScrollingControl()
        {
            InitializeComponent();
            this.DataContext = this;
            _translateTransform = FindName("tran") as TranslateTransform;
        }
      
        public static readonly DependencyProperty WidthProperty =
    DependencyProperty.Register(
        "Width",                  // 属性名称
        typeof(double),           // 属性类型
        typeof(NumberScrollingControl),         // 所属类型
        new PropertyMetadata(60.0, OnWidthChanged));             // 默认值

        private static void OnWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as NumberScrollingControl;
            control.Width = (double)e.NewValue;
        }

        public static readonly DependencyProperty HeightProperty =
            DependencyProperty.Register(
                "Height",                 // 属性名称
                typeof(double),           // 属性类型
                typeof(NumberScrollingControl),         // 所属类型
                new PropertyMetadata(20.0, OnHeightChanged));             // 默认值

        private static void OnHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as NumberScrollingControl;
            control.Height = (double)e.NewValue;
        }

        public double Width
        {
            get { return (double)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        }

        public double Height
        {
            get { return (double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }

        public static readonly DependencyProperty ForegroundProperty =
          DependencyProperty.Register(
              "Foreground",                  // 属性名称
              typeof(Brush),                 // 属性类型
              typeof(NumberScrollingControl),         // 所属类型
              new PropertyMetadata(Brushes.Black, OnForegroundChanged)); // 默认值和回调
      
        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }


        public static readonly DependencyProperty NumProperty =
        DependencyProperty.Register("Num", typeof(int), typeof(NumberScrollingControl),
            new PropertyMetadata(0, OnNumChanged));

        public int Num
        {
            get { return (int)GetValue(NumProperty); }
            set { SetValue(NumProperty, value); }
        }

        private static void OnNumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as NumberScrollingControl;
            if (control != null)
            {
                var oldValue = (int)e.OldValue;
                var newValue = (int)e.NewValue;
                if (newValue == oldValue) return;
                var animation = new DoubleAnimation()
                {
                    From = control._translateTransform.Y,
                    To = newValue*(-control.Height),
                    Duration = new Duration(TimeSpan.FromSeconds(0.5)),
                    //CubicEase：这是一个三次方缓动函数，它会根据时间的变化以非线性的方式调整动画的速度。CubicEase 会使动画在开始时加速，在结束时减速，给人一种更加自然的运动效果。
                    //EasingMode = EasingMode.EaseOut：这指定了缓动模式为“ EaseOut”，即动画在接近结束时逐渐减速。具体来说：
                    //EaseOut：动画在开始时速度较快，然后逐渐减速，最终平稳地到达目标位置。这种效果常用于模拟物体在停止前的惯性减慢。
                    //EasingMode 包括：EaseIn：动画在开始时缓慢加速，然后快速达到目标位置。EaseInOut：动画在开始和结束时都逐渐加速或减速，中间部分保持较快速度。
                    EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut }
                };
                control._translateTransform.BeginAnimation(TranslateTransform.YProperty, animation);
            }
        }

        private static void OnForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as NumberScrollingControl;
            if (control != null)
            {
                // 在这里处理 Foreground 属性变化的逻辑
                // 例如，更新控件的视觉状态或重新渲染
                control.OnForegroundChanged((Brush)e.OldValue, (Brush)e.NewValue);
            }
        }

        protected virtual void OnForegroundChanged(Brush oldForeground, Brush newForeground)
        {
            // 例如，更新控件的文本颜色
            if (this.Template != null)
            {
                var textBlock = this.Template.FindName("PART_TextBlock", this) as TextBlock;
                if (textBlock != null)
                {
                    textBlock.Foreground = newForeground;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
